﻿Public Class SP_TinhTrang
    Dim cn As New DatabaseConnect
    Dim dt As DataTable

    Private Sub SP_TinhTrang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dt = New DataTable()
        dt = cn.GetData("select sp.MaLoai,sp.tenloai,tt.tinhtrang from SanPham sp,SanPham_TinhTrang tt where sp.tinhtrang = tt.tinhtrangID AND sp.MaLoai <> -1")
        dgv.DataSource = dt
        dt = New DataTable
        dt = cn.GetData("SELECT * FROM SanPham_TinhTrang")
        cbTT.DataSource = dt
        cbTT.DisplayMember = "tinhtrang"
        cbTT.ValueMember = "tinhtrangID"
    End Sub

    Private Sub dgv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.Click
        If dgv.SelectedRows.Count > 0 Then
            pnTinhTrang.Visible = True
            lbTenSP.Text = dgv.SelectedRows(0).Cells(1).Value.ToString
            cbTT.Text = dgv.SelectedRows(0).Cells(2).Value.ToString
        Else
            Return
        End If
    End Sub

    Private Sub btnCN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCN.Click
        cn.Execute("update SanPham set tinhtrang = " & cbTT.SelectedValue & " where MaLoai = " & dgv.SelectedRows(0).Cells(0).Value & "")
        pnTinhTrang.Visible = False
        dt = New DataTable()
        dt = cn.GetData("select sp.MaLoai,sp.tenloai,tt.tinhtrang from SanPham sp,SanPham_TinhTrang tt where sp.tinhtrang = tt.tinhtrangID AND sp.MaLoai <> -1")
        dgv.DataSource = dt
        dgv.ClearSelection()
    End Sub

    Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
        dt = New DataTable()
        dt = cn.GetData("seleact sp.MaLoai,sp.tenloai,tt.tinhtrang from SanPham sp,SanPham_TinhTrang tt where sp.tinhtrang = tt.tinhtrangID AND sp.MaLoai <> -1")
        dgv.DataSource = dt
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        dt = New DataTable()
        dt = cn.GetData("select sp.MaLoai,sp.tenloai,tt.tinhtrang from SanPham sp,SanPham_TinhTrang tt where sp.tinhtrang = tt.tinhtrangID AND sp.MaLoai <> -1 AND sp.TenLoai LIKE N'%" + TextBox1.Text + "%'")
        dgv.DataSource = dt
    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            dt = New DataTable()
            dt = cn.GetData("select sp.MaLoai,sp.tenloai,tt.tinhtrang from SanPham sp,SanPham_TinhTrang tt where sp.tinhtrang = tt.tinhtrangID AND sp.MaLoai <> -1 AND sp.TenLoai LIKE N'%" + TextBox1.Text + "%'")
            dgv.DataSource = dt
        End If
    End Sub

    Private Sub dgv_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgv.KeyDown
        If e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then
            pnTinhTrang.Visible = True
            lbTenSP.Text = dgv.SelectedRows(0).Cells(1).Value.ToString
            cbTT.Text = dgv.SelectedRows(0).Cells(2).Value.ToString
        End If
    End Sub
End Class